
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>旧缓冲协议 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="具体的对象层" href="concrete.html" />
    <link rel="prev" title="缓冲协议" href="buffer.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/c-api/objbuffer.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="concrete.html" title="具体的对象层"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="buffer.html" title="缓冲协议"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python/C API 参考手册</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="abstract.html" accesskey="U">抽象对象层</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="old-buffer-protocol">
<h1>旧缓冲协议<a class="headerlink" href="#old-buffer-protocol" title="永久链接至标题">¶</a></h1>
<div class="deprecated">
<p><span class="versionmodified deprecated">3.0 版后已移除.</span></p>
</div>
<p>这些函数是 Python 2 中“旧缓冲协议”API 的组成部分。 在 Python 3 中，此协议已不复存在，但这些函数仍然被公开以便移植 2.x 的代码。 它们被用作 <a class="reference internal" href="buffer.html#bufferobjects"><span class="std std-ref">新缓冲协议</span></a> 的兼容性包装器，但它们并不会在缓冲被导出时向你提供对所获资源的生命周期控制。</p>
<p>因此，推荐你调用 <a class="reference internal" href="buffer.html#c.PyObject_GetBuffer" title="PyObject_GetBuffer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetBuffer()</span></code></a> (或者配合 <a class="reference internal" href="arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> 函数族使用 <code class="docutils literal notranslate"><span class="pre">y*</span></code> 或 <code class="docutils literal notranslate"><span class="pre">w*</span></code> <a class="reference internal" href="arg.html#arg-parsing"><span class="std std-ref">格式码</span></a>) 来获取一个对象的缓冲视图，并在缓冲视图可被释放时调用 <a class="reference internal" href="buffer.html#c.PyBuffer_Release" title="PyBuffer_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBuffer_Release()</span></code></a>。</p>
<dl class="function">
<dt id="c.PyObject_AsCharBuffer">
int <code class="sig-name descname">PyObject_AsCharBuffer</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *obj</em>, const char<em> **buffer</em>, Py_ssize_t<em> *buffer_len</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_AsCharBuffer" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个可用作基于字符的输入的只读内存地址的指针。 <em>obj</em> 参数必须支持单段字符缓冲接口。 成功时返回 <code class="docutils literal notranslate"><span class="pre">0</span></code>，将 <em>buffer</em> 设为内存地址并将 <em>buffer_len</em> 设为缓冲区长度。 出错时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code> 并设置一个 <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyObject_AsReadBuffer">
int <code class="sig-name descname">PyObject_AsReadBuffer</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *obj</em>, const void<em> **buffer</em>, Py_ssize_t<em> *buffer_len</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_AsReadBuffer" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个指向包含任意数据的只读内存地址的指针。 <em>obj</em> 参数必须支持单段可读缓冲接口。 成功时返回 <code class="docutils literal notranslate"><span class="pre">0</span></code>，将 <em>buffer</em> 设为内存地址并将 <em>buffer_len</em> 设为缓冲区长度。 出错时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code> 并设置一个 <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyObject_CheckReadBuffer">
int <code class="sig-name descname">PyObject_CheckReadBuffer</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_CheckReadBuffer" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <em>o</em> 支持单段可读缓冲接口则返回 <code class="docutils literal notranslate"><span class="pre">1</span></code>。 否则返回 <code class="docutils literal notranslate"><span class="pre">0</span></code>。 此函数总是会成功执行。</p>
<p>请注意此函数会尝试获取并释放一个缓冲区，并且在调用对应函数期间发生的异常会被屏蔽。 要获取错误报告则应改用 <a class="reference internal" href="buffer.html#c.PyObject_GetBuffer" title="PyObject_GetBuffer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetBuffer()</span></code></a>。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyObject_AsWriteBuffer">
int <code class="sig-name descname">PyObject_AsWriteBuffer</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *obj</em>, void<em> **buffer</em>, Py_ssize_t<em> *buffer_len</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_AsWriteBuffer" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个指向可写内存地址的指针。 <em>obj</em> 必须支持单段字符缓冲接口。 成功时返回 <code class="docutils literal notranslate"><span class="pre">0</span></code>，将 <em>buffer</em> 设为内存地址并将 <em>buffer_len</em> 设为缓冲区长度。 出错时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code> 并设置一个 <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。</p>
</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="buffer.html"
                        title="上一章">缓冲协议</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="concrete.html"
                        title="下一章">具体的对象层</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/c-api/objbuffer.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="concrete.html" title="具体的对象层"
             >下一页</a> |</li>
        <li class="right" >
          <a href="buffer.html" title="缓冲协议"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python/C API 参考手册</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="abstract.html" >抽象对象层</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>